package ar.edu.unicen.exa.isistan.archassis.utils;

import java.util.ArrayList;
import java.util.List;

import ar.edu.unicen.exa.isistan.archassis.diff.LastVersionDiffStrategy;
import ar.edu.unicen.exa.isistan.archassis.ia.api.AIModuleFacade;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.ItemCase;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.ListCases;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNRCSException;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNRevisionControlSystem;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNVersion;
import ar.edu.unicen.exa.isistan.archassis.source.ISourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.source.RCSSourceStrategy;

public class CasesResource {
	private static String url = "http://svn.codespot.com/a/eclipselabs.org/svn-human-diff/trunk/ar.edu.unicen.exa.isistan.plugin.";
	
	public static List<JavaClass> gatJavaClass(String url, long v0, long v1) throws SVNRCSException {
		SVNRevisionControlSystem svnRCS = new SVNRevisionControlSystem(url);
		ISourceStrategy ss = new RCSSourceStrategy(svnRCS, new SVNVersion(v0), new SVNVersion(v1));
		return  ss.getJavaClassList(new LastVersionDiffStrategy());
		
	}
	
	
	public static void load(String url, long v0, long v1, String listCaseName, String listCaseDescription, String itemCaseDetail) throws SVNRCSException {
		
		ItemCase itemCase = new ItemCase(CasesResource.gatJavaClass(url, v0, v1), itemCaseDetail);
		
		List<ItemCase> itemCaseList = new ArrayList<ItemCase>();
		itemCaseList.add(itemCase);
		
		ListCases listCases = new ListCases(listCaseName,
				itemCaseList,
				listCaseDescription);
		
		AIModuleFacade.getInstance().addListCases(listCases);
	}
	
	public static void loadItemCases(Long id, String url, long v0, long v1, String itemCaseDetail) throws Exception {
		
		ItemCase itemCase = new ItemCase(CasesResource.gatJavaClass(url, v0, v1), itemCaseDetail);
		
		AIModuleFacade.getInstance().addItemCase(id, itemCase);
	}
	
	public static void loadNewListCases(String sourceCode, String listCaseName, long v0, long v1) {
		String listCaseDescription = sourceCode + " description";
		String itemCaseDetail = sourceCode + " detail";
		
		try {
			load(url+ sourceCode, v0, v1, listCaseName, listCaseDescription, itemCaseDetail);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void loadNewListCases(String sourceCode, long v0, long v1) {
		loadNewListCases(sourceCode, sourceCode, v0, v1);
	}
	
	public static void loadNewItemCases(Long id, String sourceCode, long v0, long v1) {
		String itemCaseDetail = sourceCode + " detail";
		
		try {
			loadItemCases(id, url+ sourceCode, v0, v1, itemCaseDetail);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		loadNewListCases("command", 71, 87);
		loadNewListCases("builder", 105, 106);
		loadNewListCases("editor", 81, 93);
		loadNewItemCases(AIModuleFacade.getInstance().getCurrentId(),"FEditor", 247, 249);
		loadNewListCases("help", "plugin",107, 108);
		loadNewListCases("menu",  62, 89);
		loadNewListCases("multipage", 73, 90);
		loadNewListCases("popup", 75, 76);
		loadNewListCases("property", 77, 78);
		loadNewListCases("view", 79, 92);
		loadNewItemCases(AIModuleFacade.getInstance().getCurrentId(),"FView", 250, 251);
		loadNewListCases("perspective", 221, 225);
		//loadNewListCases("actionSets", 230, 231);
		
	}
}
